<?php

namespace App\Jobs;
use App\Models\Personal\PersonalUser;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Facades\Site;
use DB,Mail,Log;

//引入第三方微信文件
require_once(app_path('Vendor/MpWeiXin/MpWeiXin.php'));
class PersonalInterviewPush extends Job implements ShouldQueue{
    use InteractsWithQueue, SerializesModels;
    protected $interviewData;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($interviewData){
        $this->interviewData = $interviewData;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(){
        $this->config();
        $personalUser = DB::table('personal_users as t1')
            ->join('resume as t2', 't1.uid', '=', 't2.uid')
            ->select('t2.email','t2.mobile','t1.wechatmpopenid','t2.name','t1.device_token')
            ->where('t2.id',$this->interviewData->resume_id)
            ->where('t1.uid',$this->interviewData->personal_uid)
            ->first();
        //获取职位信息
        $companyJob = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->select('t1.job_title','t2.company_name','t2.address','t2.contacts','t2.mobile','t2.phone')
            ->where('t2.uid',$this->interviewData->company_uid)
            ->where('t1.id',$this->interviewData->job_id)
            ->first();
        $companyJob->name = $personalUser->name;
        $companyJob->phone = !empty($companyJob->mobile) ? $companyJob->mobile : $companyJob->phone;;
        $companyJob->interview_time =  $this->interviewData->interview_time;
        $companyJob->remark =  $this->interviewData->remark;
        $companyJob->resume_mob = $personalUser->mobile;
        $companyJob->interview_id = $this->interviewData->id;
        $companyJob->job_id = $this->interviewData->job_id;

        //检查device_token是否存在
        if (!empty($personalUser->device_token) && !empty(Site::get('personal_app_key')) &&
            !empty(Site::get('personal_master_secret'))) {
            //发送远程推送
            $job = (new PersonalInterViewApiPush($personalUser,$companyJob));
            dispatch($job);
        }
        if(!empty($personalUser->wechatmpopenid)){//检查微信是不是绑定
            //微信用户收到模板消息直接查看工作生成自动登录地址
            $jobUrl = mainUrl('mobile/jobinfo_' . idEncryption($this->interviewData->job_id)) . '.html';
            if (!empty($this->interviewData->personal_uid)) {
                $personalUser = PersonalUser::where('uid', '=', $this->interviewData->personal_uid)->first();
                if (!empty($personalUser->wechatmpopenid)) {
                    $strPassword = substr($personalUser->password, 0, 16);//取密码前16
                    $strEncryption = md5($strPassword . date('Y-W'));//拼接当前周数md5加密
                    $redirectType = 0;//验证没通过是否重定向，1是0否
                    $jobUrl .= "/?wechatmpopenid=$personalUser->wechatmpopenid&str=$strEncryption&redirect=$redirectType";
                }
            }
            $weixin = new \MpWeiXin();
            $weixin->personMessagePush($personalUser->wechatmpopenid,$companyJob,$jobUrl);
            //检查邮箱是不是存在
        }elseif(!empty($personalUser->email)){
            if(!empty(Site::get('MailAddress')) && !empty($personalUser->email)){
                Mail::send('emails.personal_interview_email', ['companyJob' => $companyJob], function ($m) use ($personalUser,$companyJob)  {
                    $m->from(Site::get('MailAddress'), Site::get('webname'));
                    $m->to($personalUser->email)->subject($companyJob->company_name.'-邀请您参加'.$companyJob->job_title.'面试');
                });
            }else{
                Log::info('发送邮件失败,e-mail:'.$personalUser->email);
            }
            //检查手机号码是不是存在
            if(!empty($personalUser->mobile) && strlen($personalUser->mobile) == '11'){
                //检查面试记录是不是被查看
                if($this->interviewData->status == 0){
                    //延迟半天推送
                    $job = (new PersonalInterviewSmsPush($companyJob))->delay(12*60*60);
                    dispatch($job);
                }
            }
        }else{
            //检查手机号码是不是存在
            if(!empty($personalUser->mobile) && strlen($personalUser->mobile) == '11'){
                //检查面试记录是不是被查看
                if($this->interviewData->status == 0){
                    //延迟半天推送
                    $job = (new PersonalInterviewSmsPush($companyJob))->delay(12*60*60);
                    dispatch($job);
                }
            }
        }

    }
}
